import { useTable, LIST_TYPE } from '@crud/index';
import { getGenCodeUnsoldList, getGenCodeSoldList } from '@/api/crm-agency/kgj/gen-code';
import { formatDateTime } from '@/utils';
// 查询
export const tabsOptions = [
  { label: '未出售', value: 0 },
  { label: '已出售', value: 1 }
];
export const codeTypeOptions = [
  { label: '全部', value: -1 },
  { label: '自主采购兑换码', value: 0 },
  { label: '赠送码', value: 1 }
];
export const memberTypeOptions = [
  { label: '全部', value: -1 },
  { label: '周卡', value: 0 },
  { label: '月卡', value: 1 },
  { label: '季卡', value: 2 },
  { label: '年卡', value: 3 }
];
export const codeStatusOptions = [
  { label: '全部', value: -1 },
  { label: '保护期', value: 1 },
  { label: '生效期', value: 2 },
  { label: '已失效', value: 3 }
];

export const codeTypeMap: Record<string, any> = { 0: '自主采购', 1: '平台赠送' };
export const exchangeTimeMap: Record<string, any> = { 0: '7天', 1: '30天', 3: '90天', 6: '180天', 12: '365天' };
export const exchangeStatusMap: Record<string, any> = { 1: '已兑换', 2: '未兑换' };
export const columns: any = [
  { label: '兑换码', prop: 'gen_code', minWidth: 160 },
  { label: '兑换码来源', prop: 'code_type', minWidth: 120, render: (s: any) => codeTypeMap[s.row.code_type] },
  { label: '会员类型', prop: 'product_type', minWidth: 120 },
  { label: '兑换时长', prop: 'exchange_time', minWidth: 120, render: (s: any) => exchangeTimeMap[s.row.exchange_time] },
  { label: '出售时间', prop: 'sold_time', minWidth: 177, render: (s: any) => s.row.sold_time ? formatDateTime(s.row.sold_time) : '--' },
  { label: '支付金额', prop: 'total_fee' },
  { label: '开始时间', prop: 'begin_time', minWidth: 177, render: (s: any) => s.row.begin_time ? formatDateTime(s.row.begin_time) : '--' },
  { label: '结束时间', prop: 'end_time', minWidth: 177, render: (s: any) => s.row.end_time ? formatDateTime(s.row.end_time) : '--' },
  { label: '兑换状态', prop: 'exchange_status', render: (s: any) => exchangeStatusMap[s.row.exchange_status] },
  { label: '兑换时间', prop: 'use_time', minWidth: 177, render: (s: any) => s.row.use_time ? formatDateTime(s.row.use_time) : '--' }

];
export const unsoldColumns: any = [
  { label: '兑换码', prop: 'gen_code', minWidth: 160 },
  { label: '兑换码来源', prop: 'code_type', minWidth: 120, render: (s: any) => codeTypeMap[s.row.code_type] },
  { label: '会员类型', prop: 'product_type', minWidth: 120 },
  { label: '订单创建时间', prop: 'add_time', type: 'dateTime', minWidth: 177 },
  { label: '支付金额', prop: 'total_fee', minWidth: 120 },
  { label: '兑换码状态', prop: 'code_satus', minWidth: 120 },
  { label: '剩余时长', prop: 'surplus_time', minWidth: 100 }
];
// 表格
export const useTableList = (options: any = {}) => {
  const timeChange = (values:any[]) => {
    let from = undefined;
    let to = undefined;
    if (values && values.length > 0) {
      from = values[0];
      to = values[1];
    }
    return { from, to };
  };
  return useTable({
    ...options,
    type: LIST_TYPE.AGENCY,
    params: {
      gen_code: '',
      code_type: -1,
      product: -1,
      code_status: -1,
      from_dt: undefined,
      to_dt: undefined,
      colonel_id: undefined,
      _dateRange: [],
      _tabActiveKey: 0
    },
    columns: unsoldColumns,
    getList: ({ data, params }:{data: Record<string, any>, params: any}) => {
      const { gen_code, code_type, product, code_status, _dateRange, _tabActiveKey, colonel_id } = data;
      let { from_dt, to_dt } = params;
      const { from, to } = timeChange(_dateRange);
      from_dt = from;
      to_dt = to;
      data = { gen_code, code_type, product, code_status, from_dt, to_dt, colonel_id };
      return _tabActiveKey === 0 ? getGenCodeUnsoldList({ data, params }) : getGenCodeSoldList({ data, params });
    }
  });
};
